<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> 
    <title> - 天地维杰网</title>
    <meta name="keywords" content="系统架构,shutdown,不与天斗,Domino,博客,程序员,架构师,笔记,技术,分享,java,Redis">
    
    <meta property="og:title" content="">
    <meta property="og:site_name" content="天地维杰网">
    <meta property="og:image" content="/img/author.jpg"> 
    <meta name="title" content=" - 天地维杰网" />
    <meta name="description" content="天地维杰网 | 博客 | 软件 | 架构 | Java "> 
    <link rel="shortcut icon" href="http://www.shutdown.cn/img/favicon.ico" />
    <link rel="apple-touch-icon" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link rel="apple-touch-icon-precomposed" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link href="http://www.shutdown.cn/js/vendor/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/main.css" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/syntax.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
     fancybox: true, 
    motion: true
  };
</script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7826003325059020" crossorigin="anonymous"></script>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container one-collumn sidebar-position-left page-home  ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-meta  custom-logo ">

  <div class="custom-logo-site-title">
    <a href="http://www.shutdown.cn"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">天地维杰网</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">人如秋鸿来有信，事若春梦了无痕</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
    <ul id="menu" class="menu">
      
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/redis/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-full"></i> <br />Redis
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/java/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-coffee"></i> <br />java
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/linux/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-linux"></i> <br />linux
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/daily/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bug"></i> <br />日常问题
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/spring/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-child"></i> <br />Spring和Springboot
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/mac/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-fire"></i> <br />Mac相关
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/middleware/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-gavel"></i> <br />中间件
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jiagou/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-rocket"></i> <br />架构
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/python/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-ship"></i> <br />python
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/front/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bolt"></i> <br />前端
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jvm/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-balance-scale"></i> <br />jvm
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/c/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-empty"></i> <br />c语言
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/web3/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-web3"></i> <br />web3
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/post/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/about/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于
          </a>
        </li>
      
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger"> <i class="menu-item-icon fa fa-search fa-fw"></i> <br /> 搜索</a>
      </li>
    </ul>
    <div class="site-search">
      <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>

    </div>
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
<section id="posts" class="posts-expand">
  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <header class="post-header">
      <h1 class="post-title" itemprop="name headline">
        <a class="post-title-link" href="http://www.shutdown.cn/post/redis4.0%E6%96%B0%E7%89%B9%E6%80%A7-%E5%86%85%E5%AD%98%E7%A2%8E%E7%89%87%E6%95%B4%E7%90%86/" itemprop="url">
        
        </a>
      </h1>
      <div class="post-meta">
      <span class="post-time">
<span class="post-meta-item-icon">
    <i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">时间：</span>
<time itemprop="dateCreated" datetime="2016-03-22T13:04:35+08:00" content="0001-01-01">
    0001-01-01
</time>
</span> 
      
      
       <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读：</span>
<span class="leancloud-visitors-count">812 字 ~4分钟</span>
</span>
      </div>
    </header>
    <div class="post-body" itemprop="articleBody">
    

    

<h2 id="what-is-active-defragmentation">What is active defragmentation?</h2>

<blockquote>
<p>什么是主动内存碎片整理？</p>
</blockquote>

<p>Active (online) defragmentation allows a Redis server to compact the spaces left between small allocations and deallocations of data in memory,  thus allowing to reclaim back memory.</p>

<blockquote>
<p>主动内存碎片整理允许一个Redis服务器在内存中的数据分配和释放过程中对产生的空间进行压缩，以便释放内存。</p>
</blockquote>

<p>Fragmentation is a natural process that happens with every allocator (but less so with Jemalloc, fortunately) and certain workloads. Normally a server restart is needed in order to lower the fragmentation, or at least to flush away all the data and create it again. However thanks to this feature implemented by Oran Agra for Redis 4.0 this process can happen at runtime in a &ldquo;hot&rdquo; way, while the server is running.</p>

<blockquote>
<p>所有分配器在一定负载下都会自然的发生碎片整理（Jemalloc 很少）。 通常，需要重启服务器以降低碎片，或至少清除所有数据然后重新创建它们。但是，由于Oran Agra在Redis 4.0中实现了这个特性，碎片整理过程可以在服务器运行时进行。</p>
</blockquote>

<p>Basically when the fragmentation is over a certain level (see the
 configuration options below) Redis will start to create new copies of the values in contiguous memory regions by exploiting certain specific Jemalloc features (in order to understand if an allocation is causing fragmentation and to allocate it in a better place), and at the same time, will release the old copies of the data. This process, repeated incrementally for all the keys
 will cause the fragmentation to drop back to normal values.</p>

<blockquote>
<p>基本上，当碎片达到一定水平（参见下面的配置选项），Redis将通过Jemalloc的功能（以了解分配是否导致碎片，并为它们分配更好的位置）并开始在临近的内存中创建新的这些值的副本，同时也将释放旧的值的副本。这个过程将重复地递增地对所有的键进行，直到碎片整理回到正常值。</p>
</blockquote>

<p>Important things to understand:</p>

<ol>
<li><p>This feature is disabled by default, and only works if you compiled Redis to use the copy of Jemalloc we ship with the source code of Redis. This is the default with Linux builds.
&gt; 这个功能默认是关闭的，而且只有在你用我们发布的带Jemalloc代码的Redis源码来编译Redis时才能使用这个功能，Linux发布版默认是带这个功能的。</p></li>

<li><p>You never need to enable this feature if you don&rsquo;t have fragmentation issues.
&gt; 如果没有碎片整理问题，你不需要启用这个功能。</p></li>

<li><p>Once you experience fragmentation, you can enable this feature when
needed with the command &ldquo;CONFIG SET activedefrag yes&rdquo;.
&gt; 你可以通过&rdquo;CONFIG SET activedefrag yes&rdquo;启用这个功能。</p></li>
</ol>

<p>The configuration parameters are able to fine tune the behavior of the defragmentation process. If you are not sure about what they mean it is a good idea to leave the defaults untouched.</p>

<blockquote>
<p>配置参数可以调整碎片整理的效果，但如果你不确定这些配置参数的含义，那么你可以让它保持默认值。</p>
</blockquote>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-text" data-lang="text"># Active defragmentation is disabled by default
# 在默认情况下，主动内存碎片整理是关闭的
# activedefrag no

# Minimum amount of fragmentation waste to start active defrag
# 开始主动内存碎片整理的最小碎片损耗量
# active-defrag-ignore-bytes 100mb

# Minimum percentage of fragmentation to start active defrag
# 开始主动内存碎片整理的最小碎片百分比
# active-defrag-threshold-lower 10

# Maximum percentage of fragmentation at which we use maximum effort
# 最大力度进行碎片整理的最大碎片率百分比
# active-defrag-threshold-upper 100

# Minimal effort for defrag in CPU percentage, to be used when the lower
# threshold is reached
# 碎片整理占用CPU下限阈值
# active-defrag-cycle-min 1

# Maximal effort for defrag in CPU percentage, to be used when the upper
# threshold is reached 
# 碎片整理占用CPU上限阈值
# active-defrag-cycle-max 25

# Maximum number of set/hash/zset/list fields that will be processed from
# the main dictionary scan
# set/hash/zset/list 类型的扫描最大元素数量，这个配置是5.0以后才有的
# active-defrag-max-scan-fields 1000</code></pre></div>
<h2 id="测试碎片整理">测试碎片整理</h2>

<p>使用配置创建redis实例</p>

<p>redis-standalone.conf</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">port 7007

bind 0.0.0.0
daemonize yes
logfile &#34;/opt/cachecloud/logs/redis-standalone-7007.log&#34;
dir &#34;/opt/cachecloud/data&#34;
dbfilename &#34;dump_7007.rdb&#34;
# 配置开启主动碎片整理
activedefrag yes
active-defrag-ignore-bytes 10mb
active-defrag-threshold-lower 2
active-defrag-threshold-upper 150
active-defrag-cycle-min 1
active-defrag-cycle-max 25
active-defrag-max-scan-fields 1000</code></pre></div>
<p>启动redis实例</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">redis-server /opt/cachecloud/conf/redis-standalone.conf</code></pre></div>
<p>插入大量数据</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#000">插入大量数据以后内存状况</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span>
<span style="color:#ce5c00;font-weight:bold">[</span>root@reids-7-104 ~<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># redis-cli -p 7007 info memory</span>
<span style="color:#8f5902;font-style:italic"># Memory</span>
used_memory:545047960
used_memory_human:519.80M
used_memory_rss:563924992
used_memory_rss_human:537.80M
allocator_frag_ratio:1.00
allocator_frag_bytes:161464
mem_fragmentation_ratio:1.03
mem_fragmentation_bytes:18938936
active_defrag_running:0

<span style="color:#ce5c00;font-weight:bold">[</span>root@reids-7-104 ~<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># redis-cli -p 7007 memory stats</span>
37<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;allocator-fragmentation.ratio&#34;</span>
38<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;1.0003455877304077&#34;</span>
39<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;allocator-fragmentation.bytes&#34;</span>
40<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">188344</span>
49<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;fragmentation&#34;</span>
50<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;1.0352247953414917&#34;</span>
51<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;fragmentation.bytes&#34;</span>
52<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">19196984</span></code></pre></div>
<p>开始执行python脚本批量删除数据</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">redis</span>
<span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">traceback</span>

<span style="color:#000">re</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">redis</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">StrictRedis</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">host</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">10.4.7.104</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">port</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">7007</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">password</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">)</span>

<span style="color:#000">key</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">jwjrand</span><span style="color:#4e9a06">&#34;</span>
<span style="color:#000">cur</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#4e9a06"></span><span style="color:#4e9a06">&#39;</span><span style="color:#4e9a06">0</span><span style="color:#4e9a06">&#39;</span>
<span style="color:#000">cou</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">1000</span>
<span style="color:#204a87;font-weight:bold">while</span> <span style="color:#000">cur</span> <span style="color:#ce5c00;font-weight:bold">!=</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">:</span>
    <span style="color:#8f5902;font-style:italic"># cur,data = re.scan(cursor=cur, match=key, count = cou)</span>
    <span style="color:#000">cur</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">data</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">re</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">execute_command</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06"></span><span style="color:#4e9a06">&#39;</span><span style="color:#4e9a06">scan</span><span style="color:#4e9a06">&#39;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">cur</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06"></span><span style="color:#4e9a06">&#39;</span><span style="color:#4e9a06">match</span><span style="color:#4e9a06">&#39;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06"></span><span style="color:#4e9a06">&#39;</span><span style="color:#4e9a06">dmntest1*</span><span style="color:#4e9a06">&#39;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">count</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#0000cf;font-weight:bold">1000</span><span style="color:#000;font-weight:bold">)</span>
    <span style="color:#204a87;font-weight:bold">try</span><span style="color:#000;font-weight:bold">:</span>
        <span style="color:#000">p</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">re</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">pipeline</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span>
        <span style="color:#204a87;font-weight:bold">for</span> <span style="color:#000">item</span> <span style="color:#204a87;font-weight:bold">in</span> <span style="color:#000">data</span><span style="color:#000;font-weight:bold">:</span>
            <span style="color:#204a87;font-weight:bold">print</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">==&gt;</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">item</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">decode</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span>
            <span style="color:#000">p</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">delete</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">item</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">decode</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span>
        <span style="color:#000">p</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">execute</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span>
        <span style="color:#204a87;font-weight:bold">print</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06"></span><span style="color:#4e9a06">&#34;</span><span style="color:#4e9a06">==&gt;</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">cur</span><span style="color:#000;font-weight:bold">)</span>
    <span style="color:#204a87;font-weight:bold">except</span> <span style="color:#c00;font-weight:bold">Exception</span> <span style="color:#204a87;font-weight:bold">as</span> <span style="color:#000">e</span><span style="color:#000;font-weight:bold">:</span>
        <span style="color:#204a87;font-weight:bold">print</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">traceback</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">format_exc</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span></code></pre></div><div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell"><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#000">数据清理过程中内存状况</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span>
<span style="color:#ce5c00;font-weight:bold">[</span>root@reids-7-104 ~<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># redis-cli -p 7007 info memory</span>
<span style="color:#8f5902;font-style:italic"># Memory</span>
used_memory:478123936
used_memory_human:455.97M
used_memory_rss:565260288
used_memory_rss_human:539.07M
allocator_frag_ratio:1.14
allocator_frag_bytes:65979840
active_defrag_running:2
<span style="color:#ce5c00;font-weight:bold">[</span>root@reids-7-104 ~<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># redis-cli -p 7007 memory stats</span>
37<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;allocator-fragmentation.ratio&#34;</span>
38<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;1.213597297668457&#34;</span>
39<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;allocator-fragmentation.bytes&#34;</span>
40<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">95918528</span>
49<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;fragmentation&#34;</span>
50<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;1.2589750289916992&#34;</span>
51<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;fragmentation.bytes&#34;</span>
52<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">116249680</span>

<span style="color:#ce5c00;font-weight:bold">[</span>root@reids-7-104 ~<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># redis-cli -p 7007 info memory</span>
<span style="color:#8f5902;font-style:italic"># Memory</span>
used_memory:375470976
used_memory_human:358.08M
used_memory_rss:564625408
used_memory_rss_human:538.47M
mem_fragmentation_ratio:1.50
mem_fragmentation_bytes:187056320
active_defrag_running:7

<span style="color:#ce5c00;font-weight:bold">[</span>root@reids-7-104 ~<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># redis-cli -p 7007 memory stats</span>
37<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;allocator-fragmentation.ratio&#34;</span>
38<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;1.5605411529541016&#34;</span>
39<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;allocator-fragmentation.bytes&#34;</span>
40<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">191159088</span>
49<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;fragmentation&#34;</span>
50<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;1.6560860872268677&#34;</span>
51<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;fragmentation.bytes&#34;</span>
52<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">223619224</span>

<span style="color:#ce5c00;font-weight:bold">[</span>root@reids-7-104 ~<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># redis-cli -p 7007 info memory</span>
<span style="color:#8f5902;font-style:italic"># Memory</span>
used_memory:265272096
used_memory_human:252.98M
used_memory_rss:557862912
used_memory_rss_human:532.02M
allocator_frag_ratio:1.76
allocator_frag_bytes:204627600
mem_fragmentation_ratio:2.09
mem_fragmentation_bytes:290492704
active_defrag_running:12

<span style="color:#ce5c00;font-weight:bold">[</span>root@reids-7-104 ~<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># redis-cli -p 7007 memory stats</span>
37<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;allocator-fragmentation.ratio&#34;</span>
38<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;1.5875883102416992&#34;</span>
39<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;allocator-fragmentation.bytes&#34;</span>
40<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">113410912</span>
49<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;fragmentation&#34;</span>
50<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;2.6743261814117432&#34;</span>
51<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;fragmentation.bytes&#34;</span>
52<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">322839920</span>

<span style="color:#ce5c00;font-weight:bold">[</span>root@reids-7-104 ~<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># redis-cli -p 7007 info memory</span>
<span style="color:#8f5902;font-style:italic"># Memory</span>
used_memory:915584
used_memory_human:894.12K
used_memory_rss:145321984
used_memory_rss_human:138.59M
mem_fragmentation_ratio:170.23
mem_fragmentation_bytes:144468304
mem_allocator:jemalloc-5.1.0
active_defrag_running:17

<span style="color:#ce5c00;font-weight:bold">[</span>root@reids-7-104 ~<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># redis-cli -p 7007 memory stats</span>
35<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;allocator-fragmentation.ratio&#34;</span>
36<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;1.2425054311752319&#34;</span>
37<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;allocator-fragmentation.bytes&#34;</span>
38<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">215048</span>
47<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;fragmentation&#34;</span>
48<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;170.23004150390625&#34;</span>
49<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;fragmentation.bytes&#34;</span>
50<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#000">144468304</span>

<span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#000">数据清理完以后内存状况</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span>
<span style="color:#ce5c00;font-weight:bold">[</span>root@reids-7-104 ~<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># redis-cli -p 7007 info memory</span>
<span style="color:#8f5902;font-style:italic"># Memory</span>
used_memory:853688
used_memory_human:833.68K
used_memory_rss:151199744
used_memory_rss_human:144.20M
used_memory_peak:545047968
allocator_frag_ratio:1.23
allocator_frag_bytes:190568
mem_fragmentation_ratio:190.96
mem_fragmentation_bytes:150407960
active_defrag_running:0
<span style="color:#ce5c00;font-weight:bold">[</span>root@reids-7-104 ~<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># redis-cli -p 7007 memory stats</span>
35<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;allocator-fragmentation.ratio&#34;</span>
36<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;1.2568612098693848&#34;</span>
37<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;allocator-fragmentation.bytes&#34;</span>
38<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">218480</span>
47<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;fragmentation&#34;</span>
48<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;190.96084594726562&#34;</span>
49<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;fragmentation.bytes&#34;</span>
50<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">(</span>integer<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#0000cf;font-weight:bold">150407960</span></code></pre></div>
<p>从清理数据过程中<code>info memory</code>和<code>memory stats</code>命令查看到的内存使用情况和内存详情可以看到，清理过程中有触发主动碎片整理，<code>active_defrag_running:17</code>， 并且碎片整理占用的CPU为17%。还可以从<code>mem_fragmentation_bytes</code>的变化过程中看到，在大量删除数据的过程中，<code>mem_fragmentation_bytes</code>因为碎片产生而逐渐变大，而在执行主动碎片整理后,<code>mem_fragmentation_bytes</code>的值逐渐变小。</p>

<p>另外<code>info</code>命令几个相关的返回值需要解释下:
- <code>mem_fragmentation_ratio</code>:  <code>used_memory_rss</code>Redis从系统申请的内存和<code>used_memory</code>Redis给数据分配的内存的比率。不仅包括碎片，还包括其他进程及代码、共享库、堆栈的内存开销等等(参见allocator_* 指标)。
- <code>mem_fragmentation_bytes</code>: <code>used_memory_rss</code> 和 <code>used_memory</code>的差值. 要注意如果的碎片量很低(几兆)但是碎片率很高，没啥事。
- <code>allocator_allocated</code>: Redis分配器分配的总内存的字节量，包含内部碎片。通常与<code>used_memory</code>量一样。
- <code>allocator_active</code>: 分配器活动页中的字节量，包含外部碎片。
- <code>allocator_resident</code>: 分配器包含的总字节量，包含可以发布到系统中的页的总字节量（通过<code>MEMORY PURGE</code>命令或者等待）。
- <code>allocator_frag_ratio</code>: <code>allocator_active</code>和<code>allocator_allocated</code>的比率。这是真实的外部碎片指标（不是mem_fragmentation_ratio）。
- <code>active_defrag_running</code>: 开启activedefrag以后，这个指标可以展示是否在执行主动碎片整理，以及执行回收将使用的CPU率.</p>

    </div>
    <footer class="post-footer">
     

     <div class="post-nav">
    <div class="post-nav-next post-nav-item">
    
        <a href="http://www.shutdown.cn/post/redis4.0%E6%96%B0%E7%89%B9%E6%80%A7-redis-modules-system/" rel="next" title="">
        <i class="fa fa-chevron-left"></i> 
        </a>
    
    </div>

    <div class="post-nav-prev post-nav-item">
    
        <a href="http://www.shutdown.cn/post/redis5.0%E4%BC%98%E5%8C%96-jemalloc5.1%E4%B8%80%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86%E5%88%86%E6%9E%90/" rel="prev" title="">
         <i class="fa fa-chevron-right"></i>
        </a>
    
    </div>
</div>
      
     
     
     






    </footer>
  </article>
</section>

          </div>
        </div>
        <div class="sidebar-toggle">
  <div class="sidebar-toggle-line-wrap">
    <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
  </div>
</div>
<aside id="sidebar" class="sidebar">
  <div class="sidebar-inner">

    <section class="site-overview sidebar-panel  sidebar-panel-active ">
      <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
        src="http://www.shutdown.cn/img/author.jpg"
        alt="不与天斗Domino" />
    <p class="site-author-name" itemprop="name">不与天斗Domino</p>
    <p class="site-description motion-element" itemprop="description"> 
        Programmer &amp; Architect</p>
</div>
      <nav class="site-state motion-element">
    <div class="site-state-item site-state-posts">
      <a href="http://www.shutdown.cn/post/">
        <span class="site-state-item-count">183</span>
        <span class="site-state-item-name">日志</span>
      </a>
    </div>
    <div class="site-state-item site-state-categories">    
        <a href="http://www.shutdown.cn/categories/">      
         
        <span class="site-state-item-count">15</span>
        
        <span class="site-state-item-name">分类</span>
        
        </a>
    </div>

    <div class="site-state-item site-state-tags">
        <a href="http://www.shutdown.cn/tags/">
         
        <span class="site-state-item-count">224</span>
        
        <span class="site-state-item-name">标签</span>
        </a>
    </div>
</nav>
      
      

      

      <div class="links-of-blogroll motion-element inline">
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=&amp;m=0&amp;s=220&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33&amp;bv=35" async="async"></script>
</div>

    </section>
    
  </div>
</aside>

      </div>
    </main>
   
    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  <span itemprop="copyrightYear">  &copy; 
  2013 - 2023</span>
  <span class="with-love"><i class="fa fa-heart"></i></span>
  <span class="author" itemprop="copyrightHolder">天地维杰网</span>
  <span class="icp" itemprop="copyrightHolder"><a href="https://beian.miit.gov.cn/" target="_blank">京ICP备13019191号-1</a></span>
</div>
<div class="powered-by">
  Powered by - <a class="theme-link" href="http://gohugo.io" target="_blank" title="hugo" >Hugo v0.63.2</a>
</div>
<div class="theme-info">
  Theme by - <a class="theme-link" href="https://github.com/xtfly/hugo-theme-next" target="_blank"> NexT
  </a>
</div>


      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
      <span id="scrollpercent"><span>0</span>%</span>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery/index.js?v=2.1.3"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/fastclick/lib/fastclick.min.js?v=1.0.6"></script> 
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.min.js?v=1.2.1"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="http://www.shutdown.cn/js/vendor/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>

<script src="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.pack.js?v=2.1.5"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/utils.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/motion.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/affix.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/schemes/pisces.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/scrollspy.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/post-details.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/toc.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/bootstrap.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
      inlineMath: [ ['$','$'] ],
      displayMath: [ ['$$','$$'] ],
      processEscapes: true
    },
    "HTML-CSS": { fonts: ["TeX"] }
  });
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML' async></script>
</body>
</html>